package com.rrqs.activity;

import com.zadtek.xmqishi.R;
import com.rrqs.activity.common.Common;
import com.rrqs.activity.common.Const;
import com.rrqs.views.ProgressWebView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

/**
 * xyzd的内置浏览器
 * @author Administrator
 *
 */
@SuppressLint("NewApi")
public class XyrrBrowserActivity extends BaseActivity {
	Intent intent;
	ProgressWebView wxxy_web_lay;
	TextView wap_title;
	private LinearLayout linear_back;
	final Activity activity = this;
	private RelativeLayout nodataid; // 网络失败没有访问到数据
	private ProgressDialog mProgress = null;
	private boolean isSuccess = true;	//访问wap页面是否成功
	private String wapUrl = "";	//wap的出现url
	private String wapTitle = "";	//wap页Title
	int startX=0;
	 int endX=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xyrr_browser_layout);
		intent = getIntent();
		wapUrl = intent.getStringExtra(Const.KEY_WAPURL_DATA);
		wapTitle = intent.getStringExtra(Const.KEY_WAPTITLE_DATA);
		
		// 将没有网络访问到的失败数据添加到面板中
		nodataid = (RelativeLayout) findViewById(R.id.connect_error);
		wxxy_web_lay = (ProgressWebView) findViewById(R.id.wxxy_web_lay);
		wap_title = (TextView) findViewById(R.id.topview);
		wap_title.setText(wapTitle);
		wxxy_web_lay.getSettings().setJavaScriptEnabled(true);
		wxxy_web_lay.getSettings().setSupportZoom(true);
		
//		wxxy_web_lay.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//		wxxy_web_lay.getSettings().setPluginState(true);
		wxxy_web_lay.getSettings().setPluginState(PluginState.ON);
		
		//设置加载进来的页面自适应手机屏幕 
		wxxy_web_lay.getSettings().setUseWideViewPort(true); 
		wxxy_web_lay.getSettings().setLoadWithOverviewMode(true);
		 //显示放大缩小控件
		wxxy_web_lay.getSettings().setBuiltInZoomControls(true);
		
		wxxy_web_lay.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				activity.setTitle("Loading...");
				activity.setProgress(progress * 100);
				if (progress == 100)
					activity.setTitle(R.string.app_name);
			}
			
			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				wap_title.setText(title);
			}
		});
		wxxy_web_lay.setWebViewClient(new WebViewClient() {

			public void onReceivedError(WebView view, int errorCode,
					String description, final String failingUrl) { // Handle the
																	// error
				// 网络访问失败
				closeProgress();
				isSuccess = false;
				Common.DisplayToast(XyrrBrowserActivity.this, "通信失败,请检查网络!", 1);
				view.stopLoading();
				nodataid.setVisibility(View.VISIBLE);
				nodataid.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						wxxy_web_lay.loadUrl(failingUrl);
						isSuccess = true;
					}
				});
			}

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
//				mProgress = BaseHelper.showProgress(XyzdBrowserActivity.this,
//						null, "加载中....", false, true);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				closeProgress();
				try {
					if (isSuccess)
						nodataid.setVisibility(View.GONE);
				} catch (Exception e) {
				}
			}

		});
		wxxy_web_lay.loadUrl(wapUrl);
		findViewById(R.id.backid).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				if (wxxy_web_lay.canGoBack()) {
//					wxxy_web_lay.goBack();
//				} else {
				wxxy_web_lay.stopLoading();
				finish();
//				}
			}
		});
		
		}


	// 关闭进度框
	void closeProgress() {
		try {
			findViewById(R.id.waitlayout).setVisibility(View.GONE);
			if (mProgress != null) {
				mProgress.dismiss();
				mProgress = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (wxxy_web_lay.canGoBack()) {
				wxxy_web_lay.goBack();
				return true;
			} else {
				wxxy_web_lay.stopLoading();
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		wxxy_web_lay.onPause();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		wxxy_web_lay.onResume();
	}

}
